package com.yungu.swift.order.thread.inform;

import com.fasterxml.jackson.databind.JsonNode;
import com.yungu.swift.order.model.dto.OrderDto;
import com.yungu.swift.utils.HttpUtils;
import com.yungu.swift.utils.JsonUtils;
import com.yungu.swift.utils.MapUtils;
import com.yungu.swift.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;

/**
 * 消息通知
 */
@Slf4j
public class OrderStatusInformThread implements Runnable {

    private String client_id = "Cjcx";
    private OrderDto orderDto;
    private String event;

    public OrderStatusInformThread(OrderDto orderDto, String event) {
        this.orderDto = orderDto;
        this.event = event;
    }

    @Override
    public void run() {
        String order_id = orderDto.getOrderNo();
        String ext_order_id = orderDto.getExtOrderId();
        if (StringUtils.isEmpty(ext_order_id)) {
            log.info("非12306订单{}", order_id);
            return;
        }
        Map<String, String> paramsMap = new HashMap<>();
        Long timestamp = System.currentTimeMillis();
        paramsMap.put("client_id", client_id);
        paramsMap.put("timestamp", timestamp.toString());
        paramsMap.put("order_id", order_id);
        paramsMap.put("ext_order_id", ext_order_id);
        paramsMap.put("event", event);
        log.info("加密参数串{}", paramsMap);
        String sign = SHA1.encode(SignUtil.createSignStr(paramsMap));
        paramsMap.put("sign", sign);
        JsonNode jsonNode;
        try {
            jsonNode = JsonUtils.readNode(JsonUtils.toJSONString(paramsMap));
        } catch (Exception e) {
            log.info("订单{}解析异常", order_id);
            return;
        }
        String callbackUrl = orderDto.getExtCallbackUrl();
        Map<String, Object> resultMap = HttpUtils.post(callbackUrl, 3, MapUtils.build("_JSON_STRING", jsonNode.toString()), null, new HashMap<>());
        boolean isRetry = true;
        int retryCount = 1;
        long sleepTime = 1000 * 60;
        while (true) {
            log.info("订单{}回调{}发起第{}次请求，签名{},结果{}", order_id, callbackUrl, retryCount, sign, resultMap);
            try {
                JsonNode result = JsonUtils.readNode(resultMap.get("result").toString());
                boolean status = result.get("success").asBoolean();
                if (status) {
                    log.info("订单{}回调{}，第{}次请求成功", order_id, callbackUrl, retryCount);
                    break;
                }
            } catch (Exception e) {
                log.info("订单{}回调{}通知异常", order_id, callbackUrl);
            }
            if (isRetry) {
                log.info("订单{}回调{}，第{}次请求失败", order_id, callbackUrl, retryCount);
                if (retryCount == 9) {
                    log.info("订单{}回调{}，请求失败,消息丢弃{}", order_id, callbackUrl, retryCount);
                    break;
                }
                retryCount++;
                try {
                    Thread.sleep(sleepTime);
                    sleepTime = sleepTime * 2;
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

    }
}
